package com.jock.zjaddressanddatepicker.pickerview.dao;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.jock.zjaddressanddatepicker.pickerview.RegionInfo;

import java.util.ArrayList;
import java.util.List;

public class RegionDAO {
	
	public static List<RegionInfo> getProvencesOrCityOnId(Activity context, int id){
		DBManager dbManager = new DBManager(context);
		SQLiteDatabase db = dbManager.openDatabase();
		List<RegionInfo> regionInfos = new ArrayList<RegionInfo>();//String.valueOf(type)
		Cursor cursor = db.rawQuery("select * from REGIONS where _id="+id,null);
	
		while (cursor.moveToNext()) {
			RegionInfo regionInfo = new RegionInfo();
			int _id = cursor.getInt(0);
			int parent = cursor.getInt(1);
			String name = cursor.getString(2);
			int type1 = cursor.getInt(3);
			regionInfo.setId(_id);
			regionInfo.setParent(parent);
			regionInfo.setName(name);
			regionInfo.setType(type1);
			regionInfos.add(regionInfo);
		}
		cursor.close();
		db.close();
		return regionInfos;
	}
	public static List<RegionInfo> getProvencesOrCity(Activity context,int type){
		DBManager dbManager = new DBManager(context);
		SQLiteDatabase db = dbManager.openDatabase();
		List<RegionInfo> regionInfos = new ArrayList<RegionInfo>();//String.valueOf(type)
		Cursor cursor = db.rawQuery("select * from REGIONS where type="+type,null);
	
		while (cursor.moveToNext()) {
			RegionInfo regionInfo = new RegionInfo();
			int _id = cursor.getInt(0);
			int parent = cursor.getInt(1);
			String name = cursor.getString(2);
			int type1 = cursor.getInt(3);
			regionInfo.setId(_id);
			regionInfo.setParent(parent);
			regionInfo.setName(name);
			regionInfo.setType(type1);
			regionInfos.add(regionInfo);
		}
		cursor.close();
		db.close();
		return regionInfos;
	}
	
	public static List<RegionInfo> getProvencesOrCityOnParent(Activity context,int parent){


		DBManager dbManager = new DBManager(context);
		SQLiteDatabase db = dbManager.openDatabase();
		List<RegionInfo> regionInfos = new ArrayList<RegionInfo>();//String.valueOf(type)
		Cursor cursor = db.rawQuery("select * from REGIONS where parent="+parent,null);

		while (cursor.moveToNext()) {
			RegionInfo regionInfo = new RegionInfo();
			int _id = cursor.getInt(0);
			int parent1 = cursor.getInt(1);
			String name = cursor.getString(2);
			int type = cursor.getInt(3);
			regionInfo.setId(_id);
			regionInfo.setParent(parent1);
			regionInfo.setName(name);
			regionInfo.setType(type);
			regionInfos.add(regionInfo);
		}
		cursor.close();
		db.close();
		return regionInfos;
	}
	
	
	//插入
	public static void insertRegion(SQLiteDatabase db, RegionInfo ri) {
		ContentValues values = new ContentValues();
		values.put("parent", ri.getParent());
		values.put("name", ri.getName());
		values.put("type", ri.getType());
		db.insert("REGIONS", null, values);
	}

	//返回所有的省市信息
	public static List<RegionInfo> queryAllInfo(Activity context) {
		DBManager dbManager = new DBManager(context);
		SQLiteDatabase db = dbManager.openDatabase();
		List<RegionInfo> regionInfos = new ArrayList<RegionInfo>();
		Cursor cursor = db.rawQuery("select * from REGIONS", null);
	
		while (cursor.moveToNext()) {
			RegionInfo regionInfo = new RegionInfo();
			int _id = cursor.getInt(cursor
					.getColumnIndex("_id"));
			int parent = cursor.getInt(cursor
					.getColumnIndex("parent"));
			String name = cursor.getString(cursor
					.getColumnIndex("name"));
			int type = cursor.getInt(cursor
					.getColumnIndex("type"));
			regionInfo.setId(_id);
			regionInfo.setParent(parent);
			regionInfo.setName(name);
			regionInfo.setType(type);

			regionInfos.add(regionInfo);
		}
		cursor.close();
		db.close();
		return regionInfos;
	}


	public static void deleteRegion(int _id, SQLiteDatabase db) {
		db.execSQL("delete from remindtable where _id = ?",
				new Object[] { _id });
	}

}
